<?php
namespace App\Http\Resources\Trade\Demand;

use App\Http\Resources\ResourceCollection;
use App\Services\Utils\TencentYun\ImageService;
use TwHelper\Filter as TwFilter;
use App\Models\Trade\Supply\SupplyStatusModel;

class DetailResource extends ResourceCollection
{

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request            
     * @return array
     */
    public function toArray($request)
    {
        $data = $this->collection->shift();
        return self::makeDataDecode($data);
    }

    /**
     * 制作使用数据（一条）
     */
    public static function makeDataDecode($data)
    {
        // 手机号
        if (isset($data['contact_mobile']) && ! empty($data['contact_mobile']) && ! self::userEntity()->isValid()) {
            $data['contact_mobile'] = substr($data['contact_mobile'], 0, 3) . '****' . substr($data['contact_mobile'], 7, 4);
        }
        // 时间
        if (isset($data['created']) && ! empty($data['created'])) {
            $data['created_view'] = TwFilter::makeDate($data['created']);
        }
        if (isset($data['updated']) && ! empty($data['updated'])) {
            $data['updated_view'] = TwFilter::makeDate($data['updated']);
        }
        if (isset($data['valided']) && ! empty($data['valided'])) {
            $data['valided_view'] = TwFilter::makeDate($data['valided']);
        }
        // 描述标签
        if (isset($data['desc_tag']) && ! empty($data['desc_tag'])) {
            $data['desc_tag'] = json_decode($data['desc_tag'], true);
        } else {
            $data['desc_tag'] = array();
        }
        // 头像
        if (isset($data['avatar']) && ! empty($data['avatar'])) {
            $data['avatar_view'] = ImageService::makeDownloadUri($data['avatar']);
        } else {
            $data['avatar'] = '';
            $data['avatar_view'] = ImageService::makeDownloadDefaultUri();
        }
        // 下架原因
        if (isset($data['leave_type']) && ! empty($data['leave_type'])) {
            $data['leave_type_view'] = self::makeLeaveType($data['leave_type']);
        }
        // 跳转链接
        // $data['uri'] = route('demandDetail', [
        // 'id' => $data['id']
        // ]);
        return $data;
    }

    /**
     * 制作编辑数据（一条）
     */
    public static function makeEditDataDecode($data)
    {
        // 描述标签
        if (isset($data['desc_tag']) && ! empty($data['desc_tag'])) {
            $data['desc_tag'] = json_decode($data['desc_tag'], true);
        } else {
            $data['desc_tag'] = array();
        }
        return $data;
    }

    /**
     * 下架原因
     */
    public static function makeLeaveType($type)
    {
        switch ($type) {
            case SupplyStatusModel::TYPE_ADMIN:
                $reason = '管理员下架';
                break;
            case SupplyStatusModel::TYPE_USER:
                $reason = '用户主动下架';
                break;
            case SupplyStatusModel::TYPE_VALIDED:
                $reason = '时间到期，自动下架';
                break;
            default:
                $reason = '未知的原因';
        }
        return $reason;
    }
}
